package com.nikitova.craps3d.view.components.game3d 
{
	import jiglib.geometry.JPlane;
	import jiglib.physics.RigidBody;
	import jiglib.plugin.papervision3d.Papervision3DPhysics;
	import jiglib.plugin.papervision3d.Pv3dMesh;

	import com.nikitova.craps3d.model.vo.MaterialDataVO;

	import org.papervision3d.core.material.TriangleMaterial;
	import org.papervision3d.core.proto.MaterialObject3D;
	import org.papervision3d.lights.PointLight3D;
	import org.papervision3d.materials.BitmapMaterial;
	import org.papervision3d.materials.ColorMaterial;
	import org.papervision3d.materials.shadematerials.FlatShadeMaterial;
	import org.papervision3d.objects.primitives.Plane;
	import org.papervision3d.view.layer.ViewportLayer;

	import flash.display.BitmapData;
	import flash.utils.Dictionary;

	/**
	 * @author schranz
	 */
	public class TableCloth3D
	{
		private var __cloth               : RigidBody;
		private var __physics             : Papervision3DPhysics;
		private var __viewportLayer       : ViewportLayer;
		private var __light            	  : PointLight3D;
		private var __material    	  	  : MaterialDataVO;
		private var __floorPlane       	  : Plane;
		private var __width 		      : Number;
		private var __height    		  : Number;



		public function TableCloth3D(vpl : ViewportLayer, physics : Papervision3DPhysics, light : PointLight3D, width:Number, height:Number, material : MaterialDataVO = null):void 
		{
			__viewportLayer  = vpl;	
			__physics 		= physics;
			__light 			= light;
			__material 		= material;
			__width 			= width;
			__height			= height;
			createCloth();
		}

		private function createClothRigidBody(material : MaterialObject3D, width : Number, height : Number) : JPlane
		{
			var surface : Plane = new Plane(material, width, height, 8, 16);
			__viewportLayer.addDisplayObject3D(surface);
			var jSurface : JPlane = new JPlane(new Pv3dMesh(surface));
			jSurface.movable = false;
			__physics.addBody(jSurface);
			return jSurface;
		}

		private function createCloth() : void
		{
			// Create floor
			var material:TriangleMaterial;
		    material = new FlatShadeMaterial(__light, 0x28A466);
		    material.doubleSided = true;
			//var materialTable		:BitmapFileMaterial = new BitmapFileMaterial("./assets/textures/craps-table.png");
			var materialTable:TriangleMaterial;
			if(!__material)
			 materialTable = new ColorMaterial(0xCCCCCC,1);
			else
			 materialTable	 = new BitmapMaterial(__material.bmd);
			materialTable.smooth = true;
			__cloth = createClothRigidBody(materialTable, __width, __height);
			__cloth.rotationZ = -90;
			__cloth.rotationX = 90;
		}


		public function get rigidBody() : RigidBody 
		{
			return __cloth;
		}

		public function get floor() : Plane 
		{
			return __floorPlane;
		}
		public function get layerIndex() : Number
		{
			return __viewportLayer.layerIndex;
		}
		
		public function get width() : Number
		{
			return __width;
		}

		public function get height() : Number
		{
			return __height;
		}

	
	}
}
